import request from '@/router/axios';
import {baseUrl} from '@/config/env';
import website from "@/config/website";
import {encrypt} from '@/util/util'

export const loginByUsername = (tenantId, account, password, type, key, code) => request({
  url: '/blade-auth/token',
  method: 'post',
  headers: {
    'Captcha-Key': key,
    'Captcha-Code': code,
  },
  params: {
    grantType: (website.captchaMode ? "captcha" : "password"),
    tenantId,
    account,
    password:encrypt(password, account),
    type
  }
});

export const loginBySocial = (tenantId, source, code, state) => request({
  url: '/blade-auth/token',
  method: 'post',
  headers: {
    'Tenant-Id': tenantId
  },
  params: {
    tenantId,
    source,
    code,
    state,
    grantType: "social",
    scope: "all",
  }
});

export const getButtons = () => request({
  url: '/blade-system/menu/buttons',
  method: 'get'
});

export const getUserInfo = () => request({
  url: baseUrl + '/user/getUserInfo',
  method: 'get'
});

export const refreshToken = (token) => request({
  url: '/blade-auth/token',
  method: 'post',
  headers: {
    // 'User-Type': 'app'
  },
  params: {
    grantType: "refresh_token",
    refreshToken: token
  }
});

export const registerGuest = (form, oauthId) => request({
  url: '/blade-user/register-guest',
  method: 'post',
  params: {
    tenantId: form.tenantId,
    name: form.name,
    account: form.account,
    password: form.password,
    oauthId
  }
});

export const getMenu = () => request({
  url: '/blade-system/menu/routes',
  method: 'get'
});

export const getCaptcha = () => request({
  url: '/blade-auth/captcha',
  method: 'get'
});

export const getTopMenu = () => request({
  url: baseUrl + '/user/getTopMenu',
  method: 'get'
});

export const sendLogs = (list) => request({
  url: baseUrl + '/user/logout',
  method: 'post',
  data: list
})

export const logout = () => request({
  // url: baseUrl + '/user/logout',
  url: '/blade-auth/logout',
  method: 'get'
})

export const onlineUser = () => request({
  // url: baseUrl + '/user/logout',
  url: '/blade-user/getOnLineUserList',
  method: 'get'
})
export const kickout = (ids) => {
  return request({
    url: '/blade-user/kickout',
    method: 'post',
    params: {
      ids,
    }
  })
}
/**
 * 请求表格接口
 * @param url 接口地址
 * @param methed  请求方式
 * @param queryParams 请求参数
 * @returns {*|AxiosPromise<any>|void}
 */
 export const getModulesTables = (url, methed, queryParams) => {
  return request({ // 这里返回的是一个Promise，request方法传入一个配置对象，配置项可参考axios
    url: url,
    method: methed,
    params: queryParams
  })
}